package com.yueya.api.senior;

import net.sf.json.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.basic.dict.model.Dict;
import com.yueya.basic.news.content.model.Content;
import com.yueya.basic.news.content.model.ContentZan;
import com.yueya.business.bed.model.Bed;
import com.yueya.business.senior.model.SeniorBase;
import com.yueya.business.senior.model.SeniorGuardian;
import com.yueya.common.constant.CodeConst;
import com.yueya.common.model.Resp;
import com.yueya.common.service.BaseService;
import com.yueya.common.util.DateUtil;
import com.yueya.common.util.UUIDGenerator;

public class ISeniorService extends BaseService {

	/**
	 * 获取列表
	 * 
	 * @param jo
	 * @return
	 */
	public Resp getPageList(JSONObject jo) {
		if (containsKey(jo, "pgn", "pgs")) {

			if (jo.containsKey("pgn")) {
				pageNumber = jo.getInt("pgn");
			}
			if (jo.containsKey("pgs")) {
				pageSize = jo.getInt("pgs");
			}
			Page page = SeniorBase.dao.paginate(pageNumber, pageSize, jo);
			if (page != null) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setData(page);
				resp.setMsg("some page data");
			} else {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("no menu data");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("参数错误");
		}
		return resp;
	}

	// 获取明细
	public Resp getDetail(JSONObject jo) {
		if (containsKey(jo, "seniorid")) {
			String id = jo.getString("seniorid");
			SeniorBase r = SeniorBase.dao.getDetailById(id);
			if (r != null) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setData(r);
			} else {
				resp.setCode(CodeConst.SUCCESS);
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("参数错误");
			
		}
		return resp;
	}


	/**
	 * 保存
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp save(JSONObject jo) {
		if (containsKey(jo, "name", "idcard")) {
			String name = jo.getString("name");
			String idcard = jo.getString("idcard");
			String gender = jo.getString("gender");
			String birthdate = jo.getString("birthdate");
			String nativeplace = jo.getString("nativeplace");
			String educational = jo.getString("educational");
			String nation = jo.getString("nation");
			String politicsstatus = jo.getString("politicsstatus");
			String marriedstatus = jo.getString("marriedstatus");
			String phonenumber = jo.getString("phonenumber");
			String foodfeature = jo.getString("foodfeature");
			String selftype = jo.getString("selftype");
			String seniortype = jo.getString("seniortype");
			String address = jo.getString("address");
			String seniornote = jo.getString("seniornote");
			String healthnote = jo.getString("healthnote");
			String nursingstandardsid = jo.getString("nursingstandardsid");
			String nursingnote = jo.getString("nursingnote");
			String bunkid = jo.getString("bunkid");
			String bodystatus = jo.getString("bodystatus");
			String guardianname = jo.getString("guardian_name");
			String guardianidcard = jo.getString("guardian_id_card");
			String guardianphone = jo.getString("guardian_phone");
			String relation = jo.getString("relation");
			String company = jo.getString("company");
			
			
			String seniorid= UUIDGenerator.getUUID();
			SeniorBase sb = Duang.duang(SeniorBase.class);
			sb.set("senior_id",seniorid );
			sb.set("name", name);
			sb.set("id_card", idcard);
			sb.set("gender", gender);
			sb.set("birth_date", birthdate);
			sb.set("native_place", nativeplace);
			sb.set("educational", educational);
			sb.set("nation", nation);
			sb.set("politics_status", politicsstatus);
			sb.set("married_status", marriedstatus);
			sb.set("phone_number", phonenumber);
			sb.set("bodystatus", bodystatus);
			sb.set("food_feature", foodfeature);
			sb.set("self_type", selftype);
			sb.set("senior_type", seniortype);
			sb.set("address", address);
			sb.set("senior_note", seniornote);
			sb.set("health_note", healthnote);
			sb.set("nursing_standards_id", nursingstandardsid);
			sb.set("nursing_note", nursingnote);
			sb.set("bunk_id", bunkid);
			sb.set("del_flag", "0");
			boolean  bl= SeniorBase.dao.save(sb);
			
			
			SeniorGuardian sg = Duang.duang(SeniorGuardian.class);
			sg.set("guardian_id", UUIDGenerator.getUUID());
			sg.set("senior_id",seniorid );
			sg.set("guardian_name",guardianname );
			sg.set("guardian_id_card",guardianidcard );
			sg.set("guardian_phone",guardianphone );
			sg.set("relation",relation );
			sg.set("company",company );
			sg.set("del_flag", "0");
			boolean  b2 = sg.save();
			
			if (bl&&b2) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("保存失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}
	
	
	/**
	 * 保存
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp update(JSONObject jo) {
		if (containsKey(jo, "name", "idcard","seniorid")) {
			String name = jo.getString("name");
			String idcard = jo.getString("idcard");
			String gender = jo.getString("gender");
			String birthdate = jo.getString("birthdate");
			String nativeplace = jo.getString("nativeplace");
			String educational = jo.getString("educational");
			String nation = jo.getString("nation");
			String politicsstatus = jo.getString("politicsstatus");
			String marriedstatus = jo.getString("marriedstatus");
			String phonenumber = jo.getString("phonenumber");
			String foodfeature = jo.getString("foodfeature");
			String selftype = jo.getString("selftype");
			String seniortype = jo.getString("seniortype");
			String address = jo.getString("address");
			String seniornote = jo.getString("seniornote");
			String healthnote = jo.getString("healthnote");
			String nursingstandardsid = jo.getString("nursingstandardsid");
			String nursingnote = jo.getString("nursingnote");
			String bunkid = jo.getString("bunkid");
			String bodystatus = jo.getString("bodystatus");
			String guardianname = jo.getString("guardian_name");
			String guardianidcard = jo.getString("guardian_id_card");
			String guardianphone = jo.getString("guardian_phone");
			String relation = jo.getString("relation");
			String company = jo.getString("company");
			
			
			String seniorid= jo.getString("seniorid");;
			SeniorBase sb = Duang.duang(SeniorBase.class);
			sb.set("senior_id",seniorid );
			sb.set("name", name);
			sb.set("id_card", idcard);
			sb.set("gender", gender);
			sb.set("birth_date", birthdate);
			sb.set("native_place", nativeplace);
			sb.set("educational", educational);
			sb.set("nation", nation);
			sb.set("politics_status", politicsstatus);
			sb.set("married_status", marriedstatus);
			sb.set("phone_number", phonenumber);
			sb.set("bodystatus", bodystatus);
			sb.set("food_feature", foodfeature);
			sb.set("self_type", selftype);
			sb.set("senior_type", seniortype);
			sb.set("address", address);
			sb.set("senior_note", seniornote);
			sb.set("health_note", healthnote);
			sb.set("nursing_standards_id", nursingstandardsid);
			sb.set("nursing_note", nursingnote);
			
			sb.set("del_flag", "0");
			boolean  bl= SeniorBase.dao.update(sb);
			
			SeniorGuardian.dao.deleteBySeniorId(seniorid);
			SeniorGuardian sg = Duang.duang(SeniorGuardian.class);
			sg.set("guardian_id", UUIDGenerator.getUUID());
			sg.set("senior_id",seniorid );
			sg.set("guardian_name",guardianname );
			sg.set("guardian_id_card",guardianidcard );
			sg.set("guardian_phone",guardianphone );
			sg.set("relation",relation );
			sg.set("company",company );
			sg.set("del_flag", "0");
		
			boolean  b2 = sg.save();
			if (bl&&b2) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("保存失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}
	
	
	/**
	 * 删除
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp delete(JSONObject jo) {
		if (containsKey(jo, "seniorid")) {
			String seniorid= jo.getString("seniorid");;
			SeniorBase r = SeniorBase.dao.getDetailById(seniorid);
			r.set("del_flag", "1");
			boolean b1= SeniorBase.dao.update(r);
			SeniorGuardian.dao.deleteBySeniorId(seniorid);
			if (b1) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("删除成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("删除失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}

}
